package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.x509.KeyIdentifier;
import sun.security.x509.SubjectKeyIdentifierExtension;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EByteTools;

import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 12/4/11
 * Time: 5:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class ESubjectKeyIdentifierExtension implements ECertExtension {
    SubjectKeyIdentifierExtension subjectKeyIdentifierExtension;
    @Override
    public String getName() {
        return Bundle.getString("ECertField.SubjectKeyIdentifier");
    }

    @Override
    public String getShortTitle() {
       String retShortTitle="";
        Enumeration elements = subjectKeyIdentifierExtension.getElements();
        while (elements.hasMoreElements())
        {
            Object o = elements.nextElement();
            if(o instanceof String)
            {
                String elementName = (String) o;
                try {
                    Object elementObject = subjectKeyIdentifierExtension.get(elementName);
                    if(elementObject == null)
                    {
                        continue;
                    }
                    if(elementObject instanceof KeyIdentifier)
                    {
                        KeyIdentifier keyId = (KeyIdentifier) elementObject;
                        retShortTitle += EByteTools.toHexString(keyId.getIdentifier()," ")+"\n";
                    }
                    else
                    {
                        retShortTitle = EByteTools.toHexString(subjectKeyIdentifierExtension.getExtensionValue()," ");
                    }

                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
        return retShortTitle;
    }

    @Override
    public String getLongTitle() {
        return getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return subjectKeyIdentifierExtension.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof SubjectKeyIdentifierExtension)
        {
            subjectKeyIdentifierExtension = (SubjectKeyIdentifierExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
